<script setup lang="ts">
import { getRoleListAPI, getRoleUserAPI, getTreeListAPI } from "@/apis/system";
import type { Role, RoleData, RoleUser, RoleUserData, RoleUserRow } from "@/types/system";
import { onMounted, ref } from "vue";
import user from "@/assets/user.svg";
import useract from "@/assets/user-active.svg";
import type { TreeInstance } from "element-plus";
import service from "@/utils/reuqest";
// 角色列表
const roleList = ref<Role[]>([]);
// 树形组件数据
const treeList = ref<RoleData[]>([]);
// 获取角色列表
const getRoleList = async () => {
  const res = await getRoleListAPI();
  // console.log("res", res);
  roleList.value = res.data;
  // 默认渲染树形组件第一项
  getRoleDetail(res.data[0].roleId);
  onTabClick(res.data[0].roleId)
};
onMounted(async () => {
  getRoleList();
  // 获取树形组件数据
  const res = await getTreeListAPI();
  //  console.log('res111',res);
  treeList.value = res.data;
});
// 树形组件的勾选列表
const perms = ref<number[][]>([]);
const treeRefs = ref<TreeInstance[]>([]);
const total = ref(0);
// 获取角色权限的勾选列表
const getRoleDetail = async (id: number) => {
  const res = await getRoleUserAPI(id);
  console.log("res", res);
  perms.value = res.data.perms;
  total.value = res.data.userTotal;
  // 设置切换时，树形组件的勾选列表，
  // 如果用default-checked-keys设置，每次切换时，不会清除之前已经选中的
  treeRefs.value.forEach((treeRef, index) => {
    treeRef.setCheckedKeys(perms.value[index]);
  });
  userid.value=res.data.roleId
};
// 点击切换角色，点谁谁高亮
const IndexList = ref<number>(0);
const userid=ref()
const Onclick = (index: number, id: number) => {
  IndexList.value = index;
  getRoleDetail(id);
  userid.value=id
  onTabClick(id)
};
// tab栏切换，设默认高亮
const activeName = ref<"tree" | "memery">("tree");
const Tabparams=ref<RoleUser>({
  page:1,
  pageSize:2
})
const UserList=ref<RoleUserRow[]>([])
const totalUser=ref()
const onTabClick =async (id:number) => {
const res=await service.get<RoleUserData>(`/park/sys/roleUser/${id}`,{
  params:Tabparams.value
})
// console.log('res',userid);
UserList.value=res.data.rows
totalUser.value=res.data.total
};
// 删除
const DelRole=async (id:number)=>{
  const res = await service.delete(`/park/sys/role/${id}`)
  getRoleList()
}
</script>

<template>
  <div class="role-container">
    <div class="left-wrapper">
      <div
        class="role-item"
        v-for="(item, index) in roleList"
        :key="index"
        @click="Onclick(index, item.roleId)"
        :class="{ active: IndexList === index }"
      >
        <div class="role-info">
          <img :src="IndexList === index ? useract : user" class="icon" />
        </div>
        <div>{{ item.roleName }}</div>
        <div class="more">
          <el-dropdown>
    <span class="el-dropdown-link">
      <img src="@/assets/more.svg" class="icon" />
        <arrow-down />
    </span>
    <template #dropdown>
      <el-dropdown-menu>
        <el-dropdown-item>
          <el-button text @click="$router.push(`/roleAdd/${item.roleId}`)">编辑角色</el-button></el-dropdown-item>
        <el-dropdown-item>
         <el-button text type="danger" @click="DelRole(item.roleId)">删除</el-button>
         </el-dropdown-item>
    </el-dropdown-menu>
    </template>
  </el-dropdown>
        </div>
      </div>
      <el-button class="addBtn" size="small" @click="$router.push('/roleAdd')">添加角色</el-button>
    </div>
    <div class="right-wrapper">
      <el-tabs v-model="activeName" class="demo-tabs">
        <el-tab-pane label="功能权限" name="tree">
          <div class="tree-wrapper">
            <div
              v-for="(item, index) in treeList"
              :key="item.id"
              class="tree-item"
            >
              <div class="tree-title">{{ item.title }}</div>
              <el-tree
                :data="item.children"
                node-key="id"
                show-checkbox
                :props="{ label: 'title', disabled: () => true }"
                default-expand-all
                ref="treeRefs"
              />
            </div></div
        ></el-tab-pane>
        <el-tab-pane :label="`人员(${totalUser})`" name="memery">
          <el-table :data="UserList">
            <el-table-column prop="id" label="序号"></el-table-column>
            <el-table-column prop="name" label="员工姓名"></el-table-column>
            <el-table-column prop="userName" label="登录账号"></el-table-column>
          </el-table>
          <el-pagination
            :total="totalUser"
            layout="total,prev,pager,next,sizes"
            :page-sizes="[1, 2, 4, 5]"
            v-model:current-page="Tabparams.page"
            v-model:page-size="Tabparams.pageSize"
            @change="onTabClick(userid)"
          />
        </el-tab-pane>
      </el-tabs>
      <!-- tree树形组件 -->

    </div>

  </div>
</template>

<style scoped lang="scss">
.role-container {
  display: flex;
  font-size: 14px;
  background-color: #fff;

  .left-wrapper {
    width: 200px;
    border-right: 1px solid #e4e7ec;
    padding: 4px;
    text-align: center;

    .role-item {
      position: relative;
      height: 56px;
      display: flex;
      align-items: center;
      justify-content: space-between;
      padding: 0 20px;
      cursor: pointer;

      &.active {
        background: #edf5ff;
        color: #4770ff;
      }
    }

    .role-info {
      display: flex;
      align-items: center;

      .icon {
        margin-right: 10px;
        width: 20px;
      }
    }

    .more {
      display: flex;
      align-items: center;
      .icon {
        width: 15px;
      }
    }

    .addBtn {
      width: 100%;
      margin-top: 20px;
    }
  }

  .right-wrapper {
    flex: 1;
    // background-color: pink;
    .tree-wrapper {
      display: flex;
      justify-content: space-between;

      .tree-item {
        flex: 1;
        border-right: 1px solid #e4e7ec;
        padding: 0px 4px;
        text-align: center;

        .tree-title {
          background: #f5f7fa;
          text-align: center;
          padding: 10px 0;
          margin-bottom: 12px;
        }
      }
    }

    ::v-deep .el-tabs__nav-wrap {
      padding-left: 20px;
    }

    .user-wrapper {
      padding: 20px;
    }
  }
}
</style>
